//
//  ZYAlreadyDownOneController.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/9.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "ZYAlreadyDownOneController.h"
#import "ZYAlreadyDownOneModel.h"
#import "ZYAlreadyDownOneCell.h"
#import "ZYAlreadyDownOneOneView.h"
#define kCell @"cell"
#import "ZYTeacherAllSuperView.h"
#import "ZYStudentDivisionController.h"
#import "HNHandleCourseVC.h"
@interface ZYAlreadyDownOneController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,strong) ZYAlreadyDownOneModel *model;


@end

@implementation ZYAlreadyDownOneController


- (NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(51,51,51)}];
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(255,255,255)}];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];

    [SVProgressHUD dismiss];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = ColorRGB(245, 245, 245);
    
    
  
    
    [self setUpSuperView];
    [self setUpRefresh];
    
    UIBarButtonItem *liftItem = [UIBarButtonItem itemWithTargat:self action:@selector(itemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
    
    self.navigationItem.leftBarButtonItem =liftItem;
    
    
    
}






- (void)itemAction{
    
    [self.navigationController popViewControllerAnimated:YES];
}




#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}

- (void)setNetWork{
    
    NSInteger type = 1;
    if (_IsPrigram) {
        type = 4;
    }
    //    http://192.168.0.113:8080/StarShine/ys/yxhUser.do
    NSString *oid = [AccountTool shareAccountTool].currentAccount.oid;
    
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?oid=%@&action=getLessonList&pageNumber=%ld&type=%ld&status=1",oid,(long)self.page,type];
    
    NSString *changeUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURL(changeUrl) Auth:YES Params:nil Success:^(id Obj) {
        
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            
            
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            NSArray *arr = [t valueForKey:@"dataList"];
            if (arr != nil && ![arr isKindOfClass:[NSNull class]] && arr.count != 0){
                
                
                for (NSDictionary *dict in arr) {
                    
                    ZYAlreadyDownOneModel *model = [ZYAlreadyDownOneModel new];
                    
                    [model setValuesForKeysWithDictionary:dict];
                    model.ID = [dict valueForKey:@"id"];
                    [self.dataSource addObject:model];
                    
                }
                
                
                
                
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
                
                
                
                
            }else{
                
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
                
                
            }
            
            
            
            
            
            
        }
        
        
        
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
    
}






- (void)setUpSuperView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(kScreenWidth *26/750, 0, kScreenWidth - (kScreenWidth *26/750)*2, kScreenHeight - 84) style:(UITableViewStyleGrouped)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.estimatedRowHeight = 200;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[ZYAlreadyDownOneCell class] forCellReuseIdentifier:kCell];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight *26/1334)];
    lineView.backgroundColor = ColorRGB(245, 245, 245);
    self.tableView.tableHeaderView = lineView;
    
    
    
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    ZYAlreadyDownOneModel *model = self.dataSource[section];
    
    return model.elementCount.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYAlreadyDownOneCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    ZYAlreadyDownOneModel *model = self.dataSource[indexPath.section];
    ZYAlreadyDownOneSubOneModel *moodelTow = model.elementCount[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    [cell assignByZYAlreadyDownOneSubOneModel:moodelTow];
    
    
    
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    ZYAlreadyDownOneModel *model = self.dataSource[section];
    
    if (model.elementCount.count > 0) {
        
        return 120  + 50 *model.teachers.count;
    }else{
        
        return 60 + 50 *model.teachers.count;
    }
    
    
    
    
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    ZYAlreadyDownOneModel *model = self.dataSource[section];
    
    
    UIView *vv = [UIView new];
    vv.backgroundColor = [UIColor whiteColor];
    UIView *oneView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - (kScreenWidth *26/750)*2, 40)];
    [vv addSubview:oneView];
    
    
    UIButton *upDownButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    upDownButton.frame = CGRectMake(0, 0,40, oneView.frame.size.height);
    [upDownButton setImage:[UIImage imageNamed:@"bg_image_xiajia_default"] forState:(UIControlStateNormal)];

    [oneView addSubview:upDownButton];
    
    
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(upDownButton.frame.origin.x + upDownButton.frame.size.width, upDownButton.frame.origin.y, oneView.frame.size.width - (upDownButton.frame.origin.x + upDownButton.frame.size.width)*2, upDownButton.frame.size.height)];
    nameLabel.text = [NSString stringWithFormat:@"%@",model.name];
    nameLabel.font = [UIFont systemFontOfSize:15];
    nameLabel.textColor = ColorRGB(51, 51, 51);
    [oneView addSubview:nameLabel];
    
    
    UIButton *pushButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    pushButton.frame = CGRectMake(oneView.frame.size.width - 50, nameLabel.frame.origin.y, 50, 50);
    [pushButton setImage:[UIImage imageNamed:@"icon_back_default"] forState:(UIControlStateNormal)];
    pushButton.tag = 1000 + section;
    [oneView addSubview:pushButton];
    
    UIImageView * lineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 40,oneView.frame.size.width, 2)];
    lineView.image = [UIImage imageNamed:@"seg_shangjia_default"];
    [vv addSubview:lineView];
    
    
    
    
    
    
    ZYTeacherAllSuperView *zyTeacher = [[ZYTeacherAllSuperView alloc]initWithFrame:CGRectMake(0, lineView.frame.origin.y + lineView.frame.size.height, oneView.frame.size.width, 50 *model.teachers.count) dataSource:model.teachers];
    
    //学生分班
    zyTeacher.pushsBlock = ^(NSString *ID) {
        
        ZYStudentDivisionController *VC = [[ZYStudentDivisionController alloc] init];
        VC.lessonId = model.ID;
        VC.teacherUid  = ID;
        [self.navigationController pushViewController:VC animated:YES];
        
        
        
    };
    
    
    
    
    
    [vv addSubview:zyTeacher];
    
    UIImageView * lineViewThree = [[UIImageView alloc] initWithFrame:CGRectMake(0,zyTeacher.frame.origin.y + zyTeacher.frame.size.height,oneView.frame.size.width, 2)];
    lineViewThree.image = [UIImage imageNamed:@"seg_shangjia_default"];
    [vv addSubview:lineViewThree];
    
    
    
    if (model.elementCount.count > 0) {
        UILabel *goodEvaluationLabel = [[UILabel alloc]initWithFrame:CGRectMake(lineViewThree.frame.origin.x, lineViewThree.frame.origin.y + lineViewThree.frame.size.height + 8, lineViewThree.frame.size.width, 20)];
        
        goodEvaluationLabel.text =@"三好评价得优统计表";
        goodEvaluationLabel.textAlignment = NSTextAlignmentCenter;
        goodEvaluationLabel.textColor = ColorRGB(153, 153, 153);
        [vv addSubview:goodEvaluationLabel];
        
        UILabel *unit = [[UILabel alloc] init];
        unit.frame = CGRectMake(0, 0, 100, goodEvaluationLabel.frame.size.height);
        unit.center = CGPointMake(kScreenWidth - 50, goodEvaluationLabel.center.y);
        unit.text = @"得优个数(个)";
        unit.font = [UIFont systemFontOfSize:12.f];
        unit.textColor = ColorRGB(153, 153, 153);
        [vv addSubview:unit];
    }
    

    
    
    
    
    
    [pushButton addTarget:self action:@selector(pushTwoButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
    
    
    return vv;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 60;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    ZYAlreadyDownOneModel *model =  self.dataSource[section];
    self.model = model;
    UIView *vv = [UIView new];
    vv.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake((kScreenWidth - (kScreenWidth *26/750)*2)/2 - 60, 5, 120, 40);
    [button setBackgroundImage:[UIImage imageNamed:@"btn_competition_default"] forState:(UIControlStateNormal)];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitle:@"开设加强课" forState:(UIControlStateNormal)];
    [button setTitleColor:ColorRGB(255, 254, 254) forState:(UIControlStateNormal)];
    
    if(!_IsPrigram)
    {
     [vv addSubview:button];
    }
    
    
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, button.frame.origin.y + button.frame.size.height + 5, kScreenWidth, 10)];
    lineLabel.backgroundColor = ColorRGB(245, 245, 245);
    [vv addSubview:lineLabel];
    
    
    
    button.tag = section + 1002;
    [button addTarget:self action:@selector(openStongbuttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
    return vv;
    
}





#pragma mark --- 编辑方法的实现
- (void)pushTwoButtonAction:(UIButton *)sender{
    
    ZYAlreadyDownOneModel *model = self.dataSource[sender.tag - 1000];
    HNHandleCourseVC *VC = [[HNHandleCourseVC alloc] init];
    VC.isProgram = self.IsPrigram;
    // 声明我这是修改课程
    VC.type = HNHandleCourseTypeEdit;
    // 对应的课程
    VC.Id = @([model.ID integerValue]);
    VC.PopOfRefresh = ^{
        
        [self.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:VC animated:YES];
    
}



#pragma mark --- 开设加强课方法的实现
- (void)openStongbuttonAction:(UIButton *)sender{
    self.model = self.dataSource[sender.tag - 1002];
    // 得三好要素得优统计表数据
    NSMutableArray <HNCourseListChartsModel *>*arr = [NSMutableArray array];
    [self.model.elementCount enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [arr addObject:[[HNCourseListChartsModel alloc] init_ValuForKeyWithDict:obj]];
    }];
    
    
    HNHandleCourseVC *VC = [[HNHandleCourseVC alloc] init];
    VC.isProgram = self.IsPrigram;
    // 声明我这是添加加强课
    VC.type = HNHandleCourseTypeStrongAdd;
    // 对应的课程
    VC.Id = @([self.model.ID integerValue]);
    // 加强课要素
    VC.strongDatas = [arr copy];
    VC.PopOfRefresh = ^{
      
        [self.tableView.mj_header beginRefreshing];
    };
    [self.navigationController pushViewController:VC animated:YES];
    
    
}







@end
